<template>
  <div class="app-container">
    <el-form :inline="true" :model="searchFormData" class="demo-form-inline" size="mini">
      <el-form-item
        label="title"
      >
        <el-input v-model="searchFormData.title" :clearable="true" placeholder="" style="width: 250px;"/>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" @click="loadTable">Search</el-button>
      </el-form-item>
    </el-form>
    <el-button type="primary" size="mini" icon="el-icon-plus" style="margin-bottom: 10px;" @click="onAdd">Add</el-button>
    <el-table
      :data="pageInfo.rows"
      border
      highlight-current-row
    >
      <el-table-column
        prop="id"
        label="id"
      />
      <el-table-column
        prop="title"
        label="title"
      />
      <el-table-column
        prop="body"
        label="body"
        :show-overflow-tooltip="true"
      />
      <el-table-column
        prop="published"
        label="published"
      >
        <template slot-scope="scope">
          {{ scope.row.published + '' }}
        </template>
      </el-table-column>
      <el-table-column>
        <template slot-scope="scope">
          <el-link type="primary" @click="onTableUpdate(scope.row)">Edit</el-link>
          |
          <el-link type="danger" @click="onTableDelete(scope.row)">Delete</el-link>
        </template>
      </el-table-column>
    </el-table>
    <el-pagination
      background
      style="margin-top: 5px"
      :current-page="searchFormData.page_index"
      :page-size="searchFormData.page_size"
      :page-sizes="[5, 10, 20, 40]"
      :total="pageInfo.total"
      layout="total, sizes, prev, pager, next"
      @size-change="onSizeChange"
      @current-change="onPageIndexChange"
    />
    <!--dialog-->
    <el-dialog
      :title="dialogTitle"
      :visible.sync="dialogVisible"
      :close-on-click-modal="false"
      @close="resetForm('dialogForm')"
    >
      <el-form
        ref="dialogForm"
        :rules="dialogFormRules"
        :model="dialogFormData"
        label-width="120px"
        size="mini"
      >
        <el-form-item
          prop="title"
          label="title"
        >
          <el-input v-model="dialogFormData.title"/>
        </el-form-item>
        <el-form-item
          prop="body"
          label="body"
        >
          <el-input v-model="dialogFormData.body" type="textarea" :rows="6" />
        </el-form-item>
        <el-form-item
          prop="published"
          label="published"
        >
          <el-switch
            v-model="dialogFormData.published"
            active-color="#13ce66"
            inactive-color="#ff4949"
            :active-value="true"
            :inactive-value="false"
          />
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="dialogVisible = false">Cancel</el-button>
        <el-button type="primary" @click="onDialogSave">Save</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
export default {
  data() {
    return {
      searchFormData: {
        title: '',
        page_index: 1,
        page_size: 10
      },
      pageInfo: {
        rows: [],
        total: 0
      },
      dialogVisible: false,
      dialogTitle: '',
      dialogFormData: {
        id: '',
        title: '',
        body: '',
        published: true
      },
      dialogFormRules: {
        title: [
          { required: true, message: 'required', trigger: 'blur' }
        ],
        body: [
          { required: true, message: 'required', trigger: 'blur' }
        ]
      }
    }
  },
  created() {
    this.loadTable()
  },
  methods: {
    loadTable() {
      this.get('/post/page', this.searchFormData, resp => {
        this.pageInfo = resp.data
      })
    },
    onTableUpdate(row) {
      this.dialogTitle = 'Edit'
      this.dialogVisible = true
      this.$nextTick(() => {
        Object.assign(this.dialogFormData, row)
      })
    },
    onTableDelete(row) {
      this.confirm(`确认要删除该记录吗？`, function() {
        const data = {
          id: row.id
        }
        this.post('/post/delete', data, () => {
          this.tip('删除成功')
          this.loadTable()
        })
      })
    },
    onDialogSave() {
      this.$refs.dialogForm.validate((valid) => {
        if (valid) {
          const uri = this.dialogFormData.id ? '/post/update' : '/post/add'
          this.post(uri, this.dialogFormData, () => {
            this.dialogVisible = false
            this.loadTable()
          })
        }
      })
    },
    onSizeChange(size) {
      this.searchFormData.page_size = size
      this.loadTable()
    },
    onAdd() {
      this.dialogTitle = 'Add'
      this.dialogVisible = true
      this.dialogFormData.id = 0
    },
    onPageIndexChange(page_index) {
      this.searchFormData.page_index = page_index
      this.loadTable()
    }
  }
}
</script>
